/*
 * Service class 
 * Created on 6 juin 2013 ( Time 15:03:31 )
 */

package org.demo.calendrier.service;

import java.util.List;

import org.demo.calendrier.bean.Calendrier;
import org.demo.calendrier.dao.JpaCalendrierDAO;
import org.telosys.starterkits.strutsjpa.EntityManagerHelper;
import org.telosys.starterkits.strutsjpa.IServices;
import javax.persistence.PersistenceException;

import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;


public class CalendrierServices implements IServices<Calendrier, Integer> {

	protected final Logger LOG = LoggerFactory.getLogger(CalendrierServices.class);

	public Calendrier load(final Integer id) {
		if (LOG.isDebugEnabled()) LOG.debug("load");
		Calendrier calendrier;
		try {
			EntityManagerHelper.beginTransaction();
			JpaCalendrierDAO calendrierDAO = new JpaCalendrierDAO();
			calendrier = calendrierDAO.findById(id);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return calendrier ;
	}

	public Calendrier save(final Calendrier entity) {
		if (LOG.isDebugEnabled()) LOG.debug("save");
		Calendrier entityNew;
		try {
			EntityManagerHelper.beginTransaction();
			JpaCalendrierDAO calendrierDAO = new JpaCalendrierDAO();
			entityNew = calendrierDAO.update(entity);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return entityNew;
	}

	public void delete(final Integer id) {
		if (LOG.isDebugEnabled()) LOG.debug("delete");
		try {
			EntityManagerHelper.beginTransaction();
			JpaCalendrierDAO calendrierDAO = new JpaCalendrierDAO();
			calendrierDAO.delete(id);
			EntityManagerHelper.commitAndCloseEntityManager();	
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
	}

	public List<Calendrier> search(final Calendrier calendrier) {
		if (LOG.isDebugEnabled()) LOG.debug("search");
		List<Calendrier> liste;
		try {
			EntityManagerHelper.beginTransaction();
			JpaCalendrierDAO calendrierDAO = new JpaCalendrierDAO();
			liste = calendrierDAO.search(calendrier);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return liste;
	}

}
